package com.softstao.guoyu.ui.activity.sale;

import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.model.sale.OrderGoods;
import com.softstao.guoyu.model.sale.RetailDetail;
import com.softstao.guoyu.mvp.interactor.sale.RetailInteractor;
import com.softstao.guoyu.mvp.presenter.sale.RetailPresenter;
import com.softstao.guoyu.mvp.viewer.sale.DeleteRetailViewer;
import com.softstao.guoyu.mvp.viewer.sale.RetailDetailViewer;
import com.softstao.guoyu.ui.adapter.OrderGoodsAdapter;
import com.softstao.softstaolibrary.library.utils.LZUtils;
import com.softstao.softstaolibrary.library.widget.CustomScrollerView;
import com.softstao.softstaolibrary.library.widget.FullyLinearLayoutManager;
import com.softstao.softstaolibrary.library.widget.LZToast;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2017/7/11.
 */

public class RetailDetailActivity extends BaseActivity implements RetailDetailViewer,DeleteRetailViewer{

    @AIPresenter(presenter = RetailPresenter.class, interactor = RetailInteractor.class)
    RetailPresenter presenter;

    @BindView(R.id.order_sn)
    TextView orderSn;
    @BindView(R.id.time)
    TextView time;
    @BindView(R.id.buyer)
    TextView buyer;
    @BindView(R.id.wechat)
    TextView wechat;
    @BindView(R.id.mobile)
    TextView mobile;
    @BindView(R.id.list_view)
    RecyclerView listView;
    @BindView(R.id.total_price)
    TextView totalPrice;
    @BindView(R.id.shipping_method)
    TextView shippingMethod;
    @BindView(R.id.shipping_num)
    TextView shippingNum;
    @BindView(R.id.scroll_view)
    CustomScrollerView scrollView;
    @BindView(R.id.loading)
    RelativeLayout loading;
    private String orderCode;
    private RetailDetail retailDetail;
    private List<OrderGoods> goodsList = new ArrayList<>();
    private OrderGoodsAdapter adapter;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public int _ContentView() {
        return R.layout.activity_retail_detail;
    }

    @Override
    public void initView() {
        initTitle("零售单详情");
        orderCode = getIntent().getStringExtra("code");

        adapter = new OrderGoodsAdapter(goodsList);
        listView.setAdapter(adapter);
        listView.setLayoutManager(new FullyLinearLayoutManager(this));
    }

    private void initData(){
        orderSn.setText("零售单编号：" + retailDetail.getRetailOrderCode());
        time.setText("时间：" + format.format(new Date(retailDetail.getDate())));
        buyer.setText("买家：" + retailDetail.getName());
        wechat.setText("微信号：" + retailDetail.getWechat());
        mobile.setText("手机号：" + retailDetail.getMobile());
        totalPrice.setText(getResources().getString(R.string.rmb) + LZUtils.priceFormat(retailDetail.getMoney()));
        shippingMethod.setText("货运方式：" + retailDetail.getMode());
        shippingNum.setText("物流编号：" + retailDetail.getLogisticsCode());
    }

    @OnClick(R.id.delete_btn)
    public void onViewClicked() {
        new AlertDialog.Builder(context)
                .setMessage("确定删除此零售单？")
                .setPositiveButton("确定",(dialog, which) -> {
                    dialog.dismiss();
                    deleteRetail();
                }).setNegativeButton("取消",(dialog, which) -> dialog.dismiss())
                .create().show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        findDetail();
    }

    @Override
    public void findDetail() {
        loading.setVisibility(View.VISIBLE);
        presenter.getRetailDetail(SharePreferenceManager.getInstance().getAgentId(),orderCode);
    }

    @Override
    public void getRetailDetail(RetailDetail retailDetail) {
        loading.setVisibility(View.GONE);
        if (retailDetail==null){
            LZToast.getInstance(context).showToast("零售单不存在！");
            finish();
        }else {
            this.retailDetail = retailDetail;
            goodsList.clear();
            goodsList.addAll(retailDetail.getProductList());
            adapter.notifyDataSetChanged();
            initData();
        }
    }

    @Override
    public void deleteRetail() {
        loading.setVisibility(View.VISIBLE);
        presenter.delete(SharePreferenceManager.getInstance().getAgentId(),retailDetail.getRetailOrderCode());
    }

    @Override
    public void deleteResult(Object result) {
        LZToast.getInstance(context).showToast("删除成功");
        finish();
    }

    @Override
    protected void error(String msg) {
        super.error(msg);
        loading.setVisibility(View.GONE);
    }
}
